package com.caszx.chargingpile.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.telecom.PhoneAccount;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

import com.caszx.chargingpile.R;
import com.caszx.chargingpile.entity.EntityTabFeeScheme;

import java.util.List;

/**
 * Created by think on 2016/1/29.
 */
public class ExcelStationView extends View {


    private Paint mLinePaint;
    private int mLineColor;
    private int mLineStrokeWidth = 2;

    private TextPaint mTextPaint;
    private int mTextColor;
    private int mTextSize;
    private float mTextHeight;

    private float mCellHeight;
    private float mCellWidth;
    private float mHeaderHeight;

    private RectF mContentRect;


    public ExcelStationView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ExcelStationView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.ExcelStationView, defStyleAttr, 0);
        mLineColor = attributes.getColor(R.styleable.ExcelStationView_excel_lineColor, Color.parseColor("#FFCCCCCC"));
        mTextColor = attributes.getColor(R.styleable.ExcelStationView_excel_textColor, Color.BLACK);
        mLineStrokeWidth = attributes.getDimensionPixelSize(R.styleable.ExcelStationView_excel_lineStrokeWidth, 2);
        mTextSize = attributes.getDimensionPixelSize(R.styleable.ExcelStationView_excel_textSize, 14);
        mCellHeight = attributes.getDimensionPixelSize(R.styleable.ExcelStationView_excel_cellHeight, 60);
        mCellWidth = attributes.getDimensionPixelSize(R.styleable.ExcelStationView_excel_cellWidth, 60);
        mHeaderHeight = attributes.getDimensionPixelSize(R.styleable.ExcelStationView_excel_headerHeight, 60);
        attributes.recycle();
        init();

    }

    public void setmLineColor(int mLineColor) {
        this.mLineColor = mLineColor;
    }

    public void setmLineStrokeWidth(int mLineStrokeWidth) {
        this.mLineStrokeWidth = mLineStrokeWidth;
    }

    private List<EntityTabFeeScheme> feeScheme;

    public void setData(List<EntityTabFeeScheme> feeScheme) {
        this.feeScheme = feeScheme;
        requestLayout();
    }

    public void setmTextColor(int mTextColor) {
        this.mTextColor = mTextColor;
    }

    public void setmTextSize(int mTextSize) {
        this.mTextSize = mTextSize;
    }

    private void init() {
        mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mLinePaint.setColor(mLineColor);
        mLinePaint.setStyle(Paint.Style.STROKE);
        mLinePaint.setStrokeWidth(mLineStrokeWidth);

        mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setColor(mTextColor);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setTextSize(mTextSize);
        Paint.FontMetrics fm = mTextPaint.getFontMetrics();
        mTextHeight = fm.bottom - fm.top;
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if(feeScheme!=null){
            int row = feeScheme.size();
            mContentRect = new RectF(getPaddingLeft(),getPaddingTop(),getPaddingLeft()+mCellWidth*4,getPaddingTop()+mHeaderHeight+mCellHeight*row);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if(feeScheme!=null){
            setMeasuredDimension(resolveMeasured(widthMeasureSpec, (int) ((mCellWidth * 4) + getPaddingLeft() + getPaddingRight())),
                    resolveMeasured(heightMeasureSpec, (int) (mCellHeight * feeScheme.size() + mHeaderHeight) + getPaddingTop() + getPaddingBottom()));
        }
    }

    private int resolveMeasured(int measureSpec, int desired)
    {
        int result = 0;
        int specSize = MeasureSpec.getSize(measureSpec);
        switch (MeasureSpec.getMode(measureSpec)) {
            case MeasureSpec.UNSPECIFIED:
                result = desired;
                break;
            case MeasureSpec.AT_MOST:
                result = Math.max(specSize, desired);
                break;
            case MeasureSpec.EXACTLY:
            default:
                result = specSize;
        }
        return result;
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBorder(canvas);
        drawText(canvas);
        drawData(canvas);

    }


    private void drawData(Canvas canvas) {
        if (feeScheme != null) {
            for (int i=0,len=feeScheme.size();i<len;i++){
                EntityTabFeeScheme entity = feeScheme.get(i);
                //时间
                canvas.drawText(entity.date,
                        getPaddingLeft()+mCellWidth / 2,
                        getPaddingTop()+mHeaderHeight + mCellHeight*i+mCellHeight/2 + ((mTextPaint.descent() - mTextPaint.ascent()) / 2-mTextPaint.descent()),
                        mTextPaint);

                //交流电费
                canvas.drawText(entity.priceAc+"",
                        getPaddingLeft()+mCellWidth + mCellWidth / 2,
                        getPaddingTop()+mHeaderHeight + mCellHeight*i+mCellHeight/2 +((mTextPaint.descent() - mTextPaint.ascent()) / 2-mTextPaint.descent()),
                        mTextPaint);

                //直流电费
                canvas.drawText(entity.priceDc+"",
                        getPaddingLeft()+mCellWidth * 2 + mCellWidth  / 2,
                        getPaddingTop()+ mHeaderHeight + mCellHeight*i+mCellHeight/2 + ((mTextPaint.descent() - mTextPaint.ascent()) / 2-mTextPaint.descent()),
                        mTextPaint);

                //服务费
                canvas.drawText(entity.serverFeeDc+"",
                        getPaddingLeft()+mCellWidth * 3 + mCellWidth  / 2,
                        getPaddingTop()+ mHeaderHeight + mCellHeight*i+mCellHeight/2 + ((mTextPaint.descent() - mTextPaint.ascent()) / 2-mTextPaint.descent()),
                        mTextPaint);
            }
        }

    }

    private void drawText(Canvas canvas) {
        canvas.drawText("时间", getPaddingLeft()+mCellWidth/2, getPaddingTop()+mHeaderHeight / 2 -((mTextPaint.descent() + mTextPaint.ascent()) / 2-mTextPaint.descent()), mTextPaint);
        canvas.drawText("交流费(元/度)", getPaddingLeft()+mCellWidth + mCellWidth  / 2,  getPaddingTop()+mHeaderHeight / 2 -((mTextPaint.descent() + mTextPaint.ascent()) / 2-mTextPaint.descent()), mTextPaint);
        canvas.drawText("直流费(元/度)", getPaddingLeft()+mCellWidth * 2 + mCellWidth  / 2, getPaddingTop()+mHeaderHeight / 2 -((mTextPaint.descent() + mTextPaint.ascent()) / 2-mTextPaint.descent()), mTextPaint);
        canvas.drawText("服务费(元/h)", getPaddingLeft()+mCellWidth * 3 + mCellWidth / 2,  getPaddingTop()+mHeaderHeight / 2 -((mTextPaint.descent() + mTextPaint.ascent()) / 2-mTextPaint.descent()), mTextPaint);
    }

    private void drawBorder(Canvas canvas) {

       canvas.drawRect(mContentRect, mLinePaint);

        if(feeScheme==null) return;
        int row = feeScheme.size();
        for (int i = 0; i < row; i++) {
            canvas.drawLine(mContentRect.left,
                    getPaddingTop()+mHeaderHeight + i * mCellHeight,
                    mContentRect.width() + getPaddingLeft(),
                    getPaddingTop()+mHeaderHeight + i * mCellHeight, mLinePaint);
        }


        for (int i = 1; i <=3; i++) {
            canvas.drawLine(getPaddingLeft()+mCellWidth*i,
                    getPaddingTop(),
                    getPaddingLeft()+mCellWidth*i,
                    mContentRect.bottom, mLinePaint);
        }
//        //第一条长竖线
//        float linelongX_01 = mCellWidth;
//        canvas.drawLine(linelongX_01, getPaddingTop(),
//                linelongX_01, mContentRect.height() + getPaddingTop(), mLinePaint);
//        //第二条长竖线
//        float linelongX_02 = mCellWidth * 3;
//        canvas.drawLine(linelongX_02, getPaddingTop(),
//                linelongX_02, mContentRect.height() + getPaddingTop(), mLinePaint);
//        //第三条长竖线
//        float linelongX_03 = mCellWidth * 5;
//        canvas.drawLine(linelongX_03, getPaddingTop(),
//                linelongX_03, mContentRect.height() + getPaddingTop(), mLinePaint);
//
//        //第一条短竖线
//        float lineshortX_01 = (linelongX_02 + linelongX_01) / 2;
//        float lineshortY_01 = mHeaderHeight / 2;
//        canvas.drawLine(lineshortX_01, lineshortY_01,
//                lineshortX_01, mContentRect.height() + getPaddingTop(), mLinePaint);
//        //第二条短竖线
//        float lineshortX_02 = (linelongX_02 + linelongX_03) / 2;
//        float lineshortY_02 = mHeaderHeight / 2;
//        canvas.drawLine(lineshortX_02, lineshortY_02,
//                lineshortX_02, mContentRect.height() + getPaddingTop(), mLinePaint);
    }
}
